<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/html" xmlns="http://www.w3.org/1999/html" xmlns="http://www.w3.org/1999/html">
<head>
    <title>修改密码</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <script src="/webjars/miniui/boot.js" type="text/javascript"></script>
    <script src="/static/js/common/common.js" type="text/javascript"></script>
    <link href="/webjars/miniui/css/demo.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="/static/js/aip/AIP_MAIN.js"></script>
    <script src="/static/js/jquery-3.6.0.min.js"></script>
    <script src="/static/js/crypto/crypto-js.js"></script>
    <link href="/static/css/default.css" rel="stylesheet" type="text/css" />
</head>
<style>
    body {
        margin:0px;
        background-color:white;
        font-family:'PT Sans', Helvetica, Arial, sans-serif;
        text-align:center;
        color:#A6A6A6;
    }
    /*输入框样式，去掉背景阴影模仿原生应用的输入框*/
    input {
        width:100%;
        height:50px;
        border:none;
        padding-left:3px;
        font-size:18px;
    }
    input:focus {
        outline:none;
    }
    /*显示隐藏密码图片*/
    img {
        width:25px;
        height:15px;
        position:absolute;
        right:60px;
        margin:5px;
    }
    /*登录按钮*/
    button {
        width:200px;
        height:50px;
        margin-top:25px;
        background:#1E90FF;
        border-radius:10px;
        border:none;
        font-size:18px;
        font-weight:700;
        color:#fff;
    }
    button:hover {
        background:#79A84B;
        outline:0;
    }
    /*输入框底部半透明横线*/
    .input_block {
        border-bottom:0px solid rgba(0, 0, 0, .1);
    }
    #page_container {
        margin:50px;
    }
</style>
<body>
<div id="form1" style="margin-top: -15px;">
    <form id="user" method="post">
        <br />
        <div style="text-align: center;padding-top: 20px;"><font style="color: #c30a0a;font-weight: bold;">密码规则：
                必须包含大小写字母、数字、字符（8-30位）</font></div>
        <br />
        <!-- 旧密码 -->
        <div class="input_block" id="psw_invisible1">
            <img id="visible1" onclick="showPsw1()" src="/static/images/password/visible.png">
            您的旧密码:<input id="input_invisible1" name="oldpwd" class="mini-password" emptyText="请输入旧密码" align="middle" required="true" /> <br /><br />
        </div>
        <!--明文密码输入框-->
        <div class="input_block" id="psw_visible1" style="display: none;">
            <img id="invisible1" onclick="hidePsw1()" src="/static/images/password/invisible.png">
            您的旧密码:<input id="input_visible1" name="oldpwd" class="mini-textbox" emptyText="请输入旧密码" align="middle" required="true" /> <br /><br />
        </div>
        <!-- 新密码 -->
        <div class="input_block" id="psw_invisible2">
            <img id="visible2" onclick="showPsw2()" src="/static/images/password/visible.png">
            您的新密码:<input id="input_invisible2" name="newpwd" class="mini-password" emptyText="请输入新密码" align="middle" required="true" type="text" />
            <br /><br />
        </div>
        <div class="input_block" id="psw_visible2" style="display: none;">
            <img id="invisible2" onclick="hidePsw2()" src="/static/images/password/invisible.png">
            您的新密码:<input id="input_visible2" name="newpwd" class="mini-textbox" emptyText="请输入新密码" align="middle" required="true" type="text" />
            <br /><br />
        </div>
        <!-- 确认新密码 -->
        <div class="input_block" id="psw_invisible3">
            <img id="visible3" onclick="showPsw3()" src="/static/images/password/visible.png">
            确认新密码:<input id="input_invisible3" name="newpwd2" class="mini-password" emptyText="请再次输入新密码" align="middle" required="true" type="text" />
            <br /><br />
        </div>
        <div class="input_block" id="psw_visible3" style="display: none;">
            <img id="invisible3" onclick="hidePsw3()" src="/static/images/password/invisible.png">
            确认新密码:<input id="input_visible3" name="newpwd2" class="mini-textbox" emptyText="请再次输入新密码" align="middle" required="true" type="text" />
            <br /><br />
        </div>
        <div style="padding:2px;margin: 0 auto;text-align: center">
            <a class="mini-button" iconCls="fa-save" plain="true" onclick="SaveData()">保存</a>
            <a href="/logout" hidden>
                <li id="loginout" class="fl">退出登录</li>
            </a>
        </div>
    </form>
</div>
<script type="text/javascript">
    mini.parse();

    //旧密码显示隐藏
    var visible1 = document.getElementById('psw_visible1');
    var invisible1 = document.getElementById('psw_invisible1');
    var inputVisible1 = mini.get("input_visible1");//明文密码
    var inputInVisible1 = mini.get("input_invisible1");//密文密码
    //新密码显示隐藏
    var visible2 = document.getElementById('psw_visible2');
    var invisible2 = document.getElementById('psw_invisible2');
    var inputVisible2 = mini.get("input_visible2");
    var inputInVisible2 = mini.get("input_invisible2");
    //确认新密码显示隐藏
    var visible3 = document.getElementById('psw_visible3');
    var invisible3 = document.getElementById('psw_invisible3');
    var inputVisible3 = mini.get("input_visible3");
    var inputInVisible3 = mini.get("input_invisible3");

    function showPsw1() {
        var val = inputInVisible1.value;
        inputVisible1.setValue(val);
        invisible1.style.display = "none";
        visible1.style.display = "";
    }

    function hidePsw1() {
        var val = inputVisible1.value;
        inputInVisible1.setValue(val);
        invisible1.style.display = "";
        visible1.style.display = "none";
    }

    function showPsw2() {
        var val = inputInVisible2.value;
        inputVisible2.setValue(val);
        invisible2.style.display = "none";
        visible2.style.display = "";
    }

    function hidePsw2() {
        var val = inputVisible2.value;
        inputInVisible2.setValue(val);
        invisible2.style.display = "";
        visible2.style.display = "none";
    }

    function showPsw3() {
        var val = inputInVisible3.value;
        inputVisible3.setValue(val);
        invisible3.style.display = "none";
        visible3.style.display = "";
    }

    function hidePsw3() {
        var val = inputVisible3.value;
        inputInVisible3.setValue(val);
        invisible3.style.display = "";
        visible3.style.display = "none";
    }

    function SaveData() {
        var form = new mini.Form("#form1");
        form.validate();
        if (form.isValid() == false) return;
        var oldpwd;
        var pwd;
        var newpwd2;

        //旧密码-显示明文
        if (invisible1.style.display == 'none') {
            oldpwd = mini.get("input_visible1").getValue();
        } else {
            //显示密文
            oldpwd = mini.get("input_invisible1").getValue();
        }
        //新密码-显示明文
        if (invisible2.style.display == 'none') {
            pwd = mini.get("input_visible2").getValue();
        } else {
            //显示密文
            pwd = mini.get("input_invisible2").getValue();
        }
        //确认新密码-显示明文
        if (invisible3.style.display == 'none') {
            newpwd2 = mini.get("input_visible3").getValue();
        } else {
            //显示密文
            newpwd2 = mini.get("input_invisible3").getValue();
        }

        if (oldpwd == pwd || oldpwd == newpwd2) {
            mini.alert("新旧密码一致!");
            return
        }

        if (pwd != newpwd2) {
            mini.alert("请输入相同密码!");
            return
        }

        /**
         * 湖南复杂度要求设置
         * @type {RegExp}
         */
        var pwdRegex = new RegExp('(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[^a-zA-Z0-9]).{8,30}');
        if (!pwdRegex.test(pwd)) {
            mini.alert("您的密码复杂度太低，请重设!");
            return
        }

        //对新旧密码进行加密 oldpwd pwd
        var encrypted = getAesString(oldpwd); //密文
        oldpwd = encrypted;
        var encrypted1 = getAesString(pwd); //密文
        pwd = encrypted1;

        $.ajax({
            url: "/basic/user/user-changepwd",
            type: "post",
            data: {
                oldpwd: oldpwd,
                pwd: pwd,
            },
            success: function (data) {
                if (data.code == 1) {
                    mini.alert("修改密码成功", "成功", function () {
                        sleep(500);
                        $("#loginout").trigger("click");
                        sleep(1000);
                    });
                } else if (data.code == 9998) {
                    mini.alert("新密码不能为空，请修改");
                } else if (data.code == 9999) {
                    mini.alert("新密码必须包含大小写字母、数字、字符（8-30位）");
                } else if (data.code == -1) {
                    mini.alert("修改密码失败!");
                }
            },
            error: function (data) {
                mini.alert("修改密码失败!");
            }
        })
    }

    function sleep(numberMillis) {
        var now = new Date();
        var exitTime = now.getTime() + numberMillis;
        while (true) {
            now = new Date();
            if (now.getTime() > exitTime)
                return;
        }
    }
</script>
</body>
</html>